import React from 'react';
import {injectIntl} from 'react-intl';
import axios from 'axios';
import {Select } from 'antd';

const Option = Select.Option;

/**
 * @remark 下拉框控件  废弃，使用SCSelect控件
 *
 */

class StubPicker extends React.Component {
    constructor(props) {
        super(props);
        const value = props.initialValue || {};
        this.state = {
            currency: value.currency || '',
            options:value.options||[]
        };
    }


    componentDidMount() {
        console.log(123123)
        this.getStuGroup(this.props.params);
    }

    getStuGroup = (params) => {
        axios.post(this.props.url, params).then(res => {
            if (res&&res.code==200) {
                if(this.props.transformData){
                    res.data=this.props.transformData(res.data)
                }
                this.setState(Object.assign(this.state.options, {options: res.data || []}))
            }
        })
    }
    componentWillReceiveProps(nextProps) {
        // Should be a controlled component.
        if ('value' in nextProps) {
            const value = nextProps.value;
            this.setState(value);
        }
    }
    handleCurrencyChange = (currency) => {
        if (!('value' in this.props)) {
            this.setState({ currency });
        }
        this.triggerChange({ currency });
    }
    triggerChange = (changedValue) => {
        const onChange = this.props.onChange;
        if (onChange) {
            onChange(changedValue);
        }
    }


    render() {
        const state = this.state;
        return (
                <Select
                    {...this.props}
                    mode={this.props.mode||"combobox"}
                    value={state.currency}
                    optionLabelProp="children" optionFilterProp="children"
                    onChange={this.handleCurrencyChange}
                    allowClear={true}
                    placeholder={this.props.placeholder||'请选择'}
                >
                    {
                        state.options.map((item, index) =>
                            <Option key={'sutbpicker' +(item.appkey||item.id) + index} value={this.props.useType ? (item.appkey || item.id) : (item.name ||item.stationName)}>{item.name ||item.stationName}</Option>
                        )
                    }
                </Select>
        )
    }
}

export default injectIntl(StubPicker);